package com.dd.admin.business.pay.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.dd.admin.business.card.entity.Card;
import com.dd.admin.business.card.service.CardService;
import com.dd.admin.business.detailServer.entity.SellDetailServer;
import com.dd.admin.business.detailServer.service.SellDetailServerService;
import com.dd.admin.business.memberCard.domain.MemberCardVo;
import com.dd.admin.business.memberCard.entity.MemberCard;
import com.dd.admin.business.memberCard.service.MemberCardService;
import com.dd.admin.business.pay.domain.BaseSell;
import com.dd.admin.business.pay.domain.BuildCardForm;
import com.dd.admin.business.pay.domain.BuildCardSell;
import com.dd.admin.business.pay.service.AddSellDetailService;
import com.dd.admin.business.pay.service.PayService;
import com.dd.admin.business.sell.entity.Sell;
import com.dd.admin.business.sell.service.SellService;
import com.dd.admin.business.sellDetail.entity.SellDetail;
import com.dd.admin.business.sellDetail.service.SellDetailService;
import com.dd.admin.business.sellPay.entity.Sellpay;
import com.dd.admin.business.sellPay.service.SellpayService;
import com.dd.admin.business.server.domain.ServerVo;
import com.dd.admin.common.exception.ApiException;
import com.dd.admin.common.utils.BigDecimalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

import static com.dd.admin.business.pay.enmus.CashierExceptionEnum.CARDNO_ALREADY_EXIST;
import static com.dd.admin.business.pay.enmus.PayConst.PRODUCT_TYPE_CARD;
import static com.dd.admin.common.consts.MemberShopConst.PUSH_FIXED;
import static com.dd.admin.common.consts.MemberShopConst.PUSH_SCALE;

@Service
public class SellDetailBuildCard implements AddSellDetailService {

    @Autowired
    private CardService cardService;
    @Autowired
    private MemberCardService memberCardService;
    @Autowired
    private SellDetailService sellDetailService;
    @Autowired
    private SellpayService sellpayService;
    @Autowired
    private PayService payService;
    @Autowired
    private SellDetailServerService sellDetailServerService;

    @Override
    public void addSellDetail(Sell sell, BaseSell sellDto) {
        //转换数据
        BuildCardSell buildCardSell = (BuildCardSell) sellDto;
        // 1 获取开卡金额 等信息 先开卡
        String cardId = buildCardSell.getBuildCardForm().getCardId();
        //获取需要的开卡信息
        Card card = cardService.getById(cardId);
        //开卡
        MemberCard memberCard = buildMemberCard(sell, buildCardSell.getBuildCardForm(), card);
        //2生成销售细单
        SellDetail sellDetail = addSellDetail(sell, buildCardSell, memberCard, card);
        //添加支付方式
        sellpayService.addSellPayList(buildCardSell.getSellPayList(), sell.getSellId());

        //4因为是开卡还要设置额外信息
        payService.setSellExtraInfo(sell.getSellId(), memberCard, BigDecimal.ZERO, BigDecimal.ZERO);
    }

    MemberCard buildMemberCard(Sell sell, BuildCardForm buildCardForm, Card card) {
        //设置卡的基本和余额信息
        MemberCard memberCard = new MemberCard();
        memberCard.setMemberId(sell.getMemberId());
        memberCard.setCardId(card.getCardId());
        memberCard.setCardName(card.getCardName());

        String cardNo = buildCardForm.getCardNo();
        MemberCardVo membercardVo = memberCardService.selectOneByCardNo(cardNo);
        if (membercardVo != null) {
            throw new ApiException(CARDNO_ALREADY_EXIST);
        }
        memberCard.setCardNo(buildCardForm.getCardNo());
        memberCard.setAmount(buildCardForm.getCardAmount());
        memberCard.setBuildCardAmount(buildCardForm.getCardAmount());
        memberCard.setGiveAmount(buildCardForm.getGiveAmount());

        //获取订单的卡折扣信息 设置进会员卡中
        memberCard.setDiscount(card.getCardDiscount());

        //设置该卡的提成金额
        memberCard.setPushMoney(card.getCardPushPercent());

        memberCardService.save(memberCard);
        return memberCard;
    }

    SellDetail addSellDetail(Sell sell, BuildCardSell buildCardSell, MemberCard membercard, Card card) {
        BuildCardForm buildCardForm = buildCardSell.getBuildCardForm();
        SellDetail sellDetail = new SellDetail();
        //3设置订单id 订单类型 订单状态
        sellDetail.setSellId(sell.getSellId());
        sellDetail.setSellType(sell.getSellType());
        sellDetail.setMemberId(sell.getMemberId());
        //设置商品信息
        sellDetail.setProductType(PRODUCT_TYPE_CARD);
        sellDetail.setCardId(buildCardForm.getCardId());
        sellDetail.setCardName(buildCardForm.getCardName());
        sellDetail.setAmount(buildCardForm.getCardAmount());
        sellDetail.setGiveAmount(buildCardForm.getGiveAmount());
        sellDetail.setRealAmount(sell.getRealAmount());
        sellDetail.setTotalRealAmount(buildCardForm.getTotalRealAmount());
        //设置会员信息
        sellDetail.setMemberCardId(membercard.getMemberCardId());

        //开卡后卡金和赠送金
        sellDetail.setAmount(membercard.getAmount());
        sellDetail.setGiveAmount(membercard.getGiveAmount());

        //保存销售细单
        sellDetailService.save(sellDetail);

            //该条目有服务人员
            List<ServerVo> serverList = buildCardForm.getServerList();
            if (CollectionUtil.isNotEmpty(serverList)) {
                //如果是固定金额 固定金额除以人数 获取固定金额
                //如果是比例 计算出比例后提成 获取固定金额
                //业绩和提成均平均分
                //总提成
                BigDecimal totalPushMoney = BigDecimal.ZERO;
                //总业绩 为订单实付金额
                BigDecimal totalGradeMoney = sellDetail.getTotalRealAmount();
                //固定金额的话
                //按比例
                if (card.getCardPushPercent() != null) {
                    //提成额 先转换为百分数
                    BigDecimal pushPercent = BigDecimalUtil.div(card.getCardPushPercent(), new BigDecimal("100"));
                    //折后价*百分数
                    totalPushMoney = BigDecimalUtil.mul(totalGradeMoney, pushPercent);
                }


                //平均分
                BigDecimal pushMoney = BigDecimalUtil.div(totalPushMoney, new BigDecimal(serverList.size()));
                BigDecimal gradeMoney = BigDecimalUtil.div(totalGradeMoney, new BigDecimal(serverList.size()));


                //设置服务人员
                serverList.stream().forEach(server -> {
                    SellDetailServer sellDetailServer = new SellDetailServer();
                    sellDetailServer.setSellId(sell.getSellId());
                    sellDetailServer.setSellDetailId(sellDetail.getSellDetailId());
                    sellDetailServer.setServerId(server.getServerId());
                    sellDetailServer.setServerName(server.getServerName());
                    sellDetailServer.setServerPushMoney(pushMoney);
                    sellDetailServer.setServerGrade(gradeMoney);
                    sellDetailServerService.save(sellDetailServer);
                });
            }
            return sellDetail;
        }
}
